<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="用户UID">
          <el-input v-model="form.uid" placeholder="用户UID" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="form.pledge_type" placeholder="类型">
            <el-option
              v-for="item in typeList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="form.status" placeholder="状态">
            <el-option
              v-for="item in status"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onExport">导出</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column prop="uid" label="用户UID" align="center" />
          <el-table-column
            prop="pledge_type"
            label="类型"
            align="center"
            :formatter="formatterPledgeType"
          />
          <el-table-column
            prop="identity"
            label="身份"
            align="center"
            :formatter="formatterIdentity"
          />
          <el-table-column prop="coin_name" label="质押币种" align="center" />
          <el-table-column prop="amount" label="直接数量" align="center" />
          <el-table-column prop="fee" label="手续费" align="center" />
          <el-table-column prop="duration" label="持续时间" align="center" />
          <el-table-column prop="bill_id" label="成功账单ID" align="center" />
          <el-table-column
            prop="status"
            label="状态"
            align="center"
            :formatter="formatterStatus"
          />
          <el-table-column
            prop="confirm_at"
            label="确认时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
          <el-table-column
            prop="createtime"
            label="创建时间"
            align="center"
            width="180"
            :formatter="formatterTime"
          />
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { formatterTime, exportToExcel,timestamp } from "@/utils/utils";
import { pledgehistoryApi } from "@/api/moby/nft/nft";

const loading = ref(false);
const tableData = ref([]);
// 类型
const typeList = [
  { type: 0, name: "续费" },
  { type: 1, name: "新增" },
  { type: 2, name: "赎回" },
];
// 状态
const status = [
  { type: 0, name: "待确认" },
  { type: 1, name: "成功" },
  { type: 2, name: "失败" },
];
const formData = {
  uid: "",
  pledge_type: "",
  status: "",
  timeData: [],
};
const form = reactive({ ...formData });
const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    pledge_type: form.pledge_type,
    uid: form.uid,
    status: form.status,
    start: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
  };
};
// 状态
const formatterStatus = (row, column, cellValue, index) => {
  let name = "";
  status.filter((item) => {
    if (item.type === cellValue) {
      name = item.name;
    }
  });
  return name;
};
// 类型
const formatterPledgeType = (row, column, cellValue, index) => {
  let name = "";
  typeList.filter((item) => {
    if (item.type === cellValue) {
      name = item.name;
    }
  });
  return name;
};
// 身份
const formatterIdentity = (row, column, cellValue, index) => {
  const obj = {
    0: "-",
    1: "member",
    2: "creator",
  };

  return obj[cellValue];
};
// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await pledgehistoryApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;
  } catch (error) {
    loading.value = false;
  }
};

// 重置
const onReset = () => {
  Object.assign(form, formData);
};
// 查询
const onQuery = async () => {
  getTbaleData();
};
// 导出
const onExport = async () => {
  const data = Object.assign(param(), { export: 1 });
  const res = await pledgehistoryApi(data);

  let head = {
    id: "ID",
    uid: "用户UID",
    pledge_type: "类型",
    identity: "身份",
    coin_name: "质押币种",
    amount: "直接数量",
    fee: "手续费",
    duration: "持续时间",
    bill_id: "成功账单ID",
    status: "状态",
    confirm_at: "确认时间",
    createtime: "创建时间",
  };
  exportToExcel(res.data.results, head, "Moby-质押历史");
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};

// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>
